package com.hh.controller;

import com.hh.dto.ResultDTO;
import com.hh.service.CodeService;
import com.hh.service.UserService;
import com.hh.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class LoginController {
    private String sessionCode;
    @Autowired
    private UserService userService;
    @Autowired
    private CodeService codeService;

    @PostMapping("/login")
    public ResultDTO login(UserVO userVO, HttpServletRequest req) {
        UserVO vo = new UserVO();
        vo.setUsername(userVO.getUsername());
        vo.setPassword(userVO.getPassword());
        vo.setCode(userVO.getCode());
        vo.setSessionCode(sessionCode);
        ResultDTO resultDTO = userService.login(vo);
        if (resultDTO.getMsg().equals("登录成功！")) {
            req.getSession().setAttribute("username", userVO.getUsername());
        }
        return resultDTO;
    }

    @GetMapping("/code")
    public void code(HttpServletRequest request, HttpServletResponse response) throws IOException {
        codeService.getCode(request,response);
        sessionCode = (String) request.getSession().getAttribute("vCode");
        System.out.println(sessionCode);
    }
}
